package com.lik888.web1.Scheduled;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lik888.web1.entity.GoodsTable;
import com.lik888.web1.entity.nums.Status;
import com.lik888.web1.service.IGoodsTableService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class RedisScheduled {
    private final StringRedisTemplate stringRedisTemplate;
    private final IGoodsTableService goodsTableService;

    // 每半小时更新一次
    @Scheduled(cron = "0 */30 * * * ?")
    public boolean redisSchedule() {
        // 原有预热逻辑不变...
        SetOperations<String, String> stringStringSetOperations = stringRedisTemplate.opsForSet();
        List<GoodsTable> goodsTableList = goodsTableService.list(
                new LambdaQueryWrapper<GoodsTable>().eq(GoodsTable::getGoodsStatus, Status.NORMAL)
        );
        goodsTableList.stream()
                .map(GoodsTable::getGoodsId)
                .forEach(id -> stringStringSetOperations.add("goods", id.toString()));

        Long size = stringStringSetOperations.size("goods");
        stringRedisTemplate.opsForValue().set("total", size.toString());
        return true;
    }

}
